# encoding:utf-8

import RPi.GPIO as GPIO
import time

TRLG = 2
ECHO = 3
def dist_init():
	GPIO.setmode(GPIO.BCM)
	GPIO.setwarnings(False)
	#3号脚，GPIO2
	GPIO.setup(TRLG,GPIO.OUT,initial=GPIO.LOW)
	#5号脚，GPIO3
	GPIO.setup(ECHO,GPIO.IN)
	time.sleep(2)

def checkdistance():
	#发出触发信号
	GPIO.output(TRLG,GPIO.HIGH)
	#保持15us
	time.sleep(0.000015)
	GPIO.output(TRLG,GPIO.LOW)
	while not GPIO.input(ECHO):
		pass
	#发现高电平时开始计时
	t1 = time.time()
	while GPIO.input(ECHO):
		pass
	#高电平时结束计时
	t2 = time.time()
	#返回距离米
	return (t2 - t1)*340/2

dist_init()
while True:
	print 'distance: %0.2f m' %checkdistance()
	time.sleep(1)
